package com.lyz.myself.service.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyz.myself.config.utils.util.DateUtil;
import com.lyz.myself.controller.BaseController;
import com.lyz.myself.mapper.CompanyMapper;
import com.lyz.myself.pojo.Po.Company;
import com.lyz.myself.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: 机构相关信息实现类
 * @Date: 2019/1/14 15:46
 * @Author: Mr.Deng
 * @Description:
 */
@Service
public class CompanyServiceImpl extends BaseController implements CompanyService {
    @Resource
    CompanyMapper companyMapper;


    /**
     * 查看所有机构信息
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<Map<String, String>> selectAllCompany(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, String>> companyList = companyMapper.selectAllCompany();
        PageInfo<Map<String, String>> page = new PageInfo<>(companyList);
        System.out.println();
        return page;
    }

    @Override
    public int deleteByPrimaryKey(Integer company_id) {
        return companyMapper.deleteByPrimaryKey(company_id);
    }

    /**
     * 增加机构信息
     *
     * @param company
     * @return
     */
    @Transactional
    @Override
    public boolean insertSelective(Company company) {
        company.setAdd_time(DateUtil.timeMillis());
        company.setAdd_user(getUser().getUser_id());
        company.setUseable(1);
        company.setDelete_type(0);
        boolean boo;
        int insertCompany = companyMapper.insertSelective(company);
        if (insertCompany > 0) {
            boo = true;
        } else {
            boo = false;
        }
        return boo;
    }

    @Override
    public Company selectByPrimaryKey(Integer company_id) {
        return companyMapper.selectByPrimaryKey(company_id);
    }

    /**
     * 修改机构信息
     *
     * @param company
     * @return
     */
    @Transactional
    @Override
    public boolean updateByPrimaryKeySelective(Company company) {
        company.setModify_time(DateUtil.timeMillis());
        company.setModify_user(getUser().getUser_id());
        boolean boo;
        int updateCompay = companyMapper.updateByPrimaryKeySelective(company);
        if (updateCompay > 0) {
            boo = true;
        } else {
            boo = false;
        }
        return boo;
    }

    /**
     * 删除机构
     *
     * @param company_id
     * @return
     */
    @Transactional
    @Override
    public boolean deleteCompany(String company_id) {
        List<Integer> list = new ArrayList<Integer>();
        String[] str = company_id.split(",");
        Integer newTime = 0;
        for (int i = 0; i < str.length; i++) {
            list.add(Integer.parseInt(str[i]));
            newTime = DateUtil.timeMillis();
        }
        boolean boo;
        int deleteCompay = companyMapper.deleteCompany(list, newTime, getUser().getUser_id());
        if (deleteCompay > 0) {
            boo = true;
        } else {
            boo = false;
        }
        return boo;
    }
}
